package com.stylefeng.guns.biz.api.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.biz.wxchat.model.TWechatUser;
import com.stylefeng.guns.biz.wxchat.service.ITWechatUserService;
import com.stylefeng.guns.core.result.BaseResultVo;
import com.stylefeng.guns.core.result.JsonReturnHelper;
import com.stylefeng.guns.modular.platform.model.TaskRewardFlows;
import com.stylefeng.guns.modular.platform.model.UserTask;
import com.stylefeng.guns.modular.platform.model.UserTaskFlows;
import com.stylefeng.guns.modular.platform.model.UserTaskVo;
import com.stylefeng.guns.modular.platform.service.ITaskRewardFlowsService;
import com.stylefeng.guns.modular.platform.service.IUserTaskFlowsService;
import com.stylefeng.guns.modular.platform.service.IUserTaskService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务相关服务
 */
@Controller
@EnableScheduling
@RequestMapping("/api/mini/")
public class MiniTaskController {

    @Autowired
    private IUserTaskService iUserTaskService;

    @Autowired
    private IUserTaskFlowsService iUserTaskFlowsService;

    @Autowired
    private ITaskRewardFlowsService iTaskRewardFlowsService;

    @Autowired
    private ITWechatUserService iTWechatUserService;

    @RequestMapping("getUserTaskList")
    @ResponseBody
    public BaseResultVo getUserTaskList(Integer userId){

        Map<String, Object> hashMap = new HashMap<>();


        List<UserTaskVo> userTaskList = iUserTaskService.selectUserTaskList();
        if (CollectionUtils.isNotEmpty(userTaskList)) {
            for (UserTaskVo userTaskVo : userTaskList) {

                //没有用户信息直接设置完成度为0
                if (userId == null) {
                    userTaskVo.setCompleteTaskNums(0);
                    continue;
                }

                UserTaskFlows userTaskFlows = new UserTaskFlows();
                userTaskFlows.setUserId(userId);
                userTaskFlows.setUserTaskId(userTaskVo.getId());
                List<UserTaskFlows> userTaskFlowsList = iUserTaskFlowsService.selectByParams(userTaskFlows);
                if (CollectionUtils.isNotEmpty(userTaskFlowsList)) {
                    userTaskVo.setCompleteTaskNums(userTaskFlowsList.get(0).getCompleteNums());
                } else {
                    userTaskVo.setCompleteTaskNums(0);

                    //该用户没有任务记录则新增一条
                    userTaskFlows.setCompleteNums(0);
                    userTaskFlows.setUpdateTime(new Date());
                    iUserTaskFlowsService.insert(userTaskFlows);
                }

                TaskRewardFlows taskRewardFlows = new TaskRewardFlows();
                taskRewardFlows.setUserId(userId);
                taskRewardFlows.setUserTaskId(userTaskVo.getId());
                taskRewardFlows.setCreateTime(new Date());
                int count = iTaskRewardFlowsService.getTaskRewardReceiveInfo(taskRewardFlows);
                if (count != 0) {
                    userTaskVo.setReceiveStatus(true);
                }
            }
        }

        hashMap.put("userTaskList", userTaskList);

        return JsonReturnHelper.getSuccessWithData(hashMap);
    }

    /**
     * 领取任务奖励
     * @param userId
     * @param userTaskId
     * @return
     */
    @RequestMapping("receiveTaskReward")
    @ResponseBody
    public BaseResultVo receiveTaskReward(Integer userId,Integer userTaskId){

        Map<String, Object> hashMap = new HashMap<>();

        if (userId == null || userTaskId == null) {
            hashMap.put("receiveStatus", false);
            hashMap.put("receiveMsg", "领取失败！");
        } else {
            TaskRewardFlows taskRewardFlows = new TaskRewardFlows();
            taskRewardFlows.setUserId(userId);
            taskRewardFlows.setUserTaskId(userTaskId);
            taskRewardFlows.setCreateTime(new Date());

            int count = iTaskRewardFlowsService.getTaskRewardReceiveInfo(taskRewardFlows);

            if (count == 0){
                UserTask userTask = iUserTaskService.selectById(userTaskId);
                if (userTask != null) {
                    taskRewardFlows.setGoldCoin(userTask.getTaskReward());
                    iTaskRewardFlowsService.insert(taskRewardFlows);

                    //更新用户金币
                    //增加金额
                    TWechatUser tWechatUser = new TWechatUser();
                    tWechatUser.setId(userId);
                    tWechatUser.setGoldCoin(userTask.getTaskReward());
                    iTWechatUserService.updateGoldCoin(tWechatUser);
                }
            }

            hashMap.put("receiveStatus", true);
        }

        return JsonReturnHelper.getSuccessWithData(hashMap);
    }

    /**
     * 更新任务完成度
     * @return
     */
    @RequestMapping("updateUserTaskComplete")
    @ResponseBody
    public BaseResultVo updateUserTaskComplete(Integer userId,Integer taskType){

        HashMap<String, Object> hashMap = new HashMap<>();

        if (userId == null || taskType == null) {
            hashMap.put("retCode", "0000001");
            hashMap.put("retMsg", "userId/taskType不允许为空");
        } else {

            UserTaskFlows userTaskFlows = new UserTaskFlows();
            userTaskFlows.setUserId(userId);

            //应需求写死taskType匹配，任务类型为1，则同时更新任务玩两个游戏、玩五个游戏
            if (taskType == 1){
                //两个游戏userTaskId=1
                userTaskFlows.setUserTaskId(1);
                this.updateTaskComplete(userTaskFlows);

                //五个游戏userTaskId=2
                userTaskFlows.setUserTaskId(2);
                this.updateTaskComplete(userTaskFlows);
            }

            //任务类型为2，更新邀请好友的userTaskId=3
            if (taskType == 2) {
                userTaskFlows.setUserTaskId(3);
                this.updateTaskComplete(userTaskFlows);
            }

            hashMap.put("retCode", "0000000");
            hashMap.put("retMsg", "更新成功");
        }


        return JsonReturnHelper.getSuccessWithData(hashMap);
    }

    /**更新任务完成度
     *
     */
    private void updateTaskComplete(UserTaskFlows userTaskFlows) {

        //如果没有相应的任务信息，则新增一条记录
        Map<String, Object> reqParams = new HashMap<>();
        reqParams.put("userId", userTaskFlows.getUserId());
        reqParams.put("userTaskId", userTaskFlows.getUserTaskId());
        List<UserTaskFlows> taskFlows = iUserTaskFlowsService.selectByMap(reqParams);
        if (CollectionUtils.isEmpty(taskFlows)) {
            userTaskFlows.setCompleteNums(0);
            userTaskFlows.setUpdateTime(new Date());
            iUserTaskFlowsService.insert(userTaskFlows);
        }

        iUserTaskFlowsService.updateUserTaskFlowdComplete(userTaskFlows);
    }

}
